﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "IType.h"
#include "IElement.h"
#include "ElementId.h"
#include "IDocument.h"
#include "ISelection.h"
#include "IUiDocument.h"
#include "IMainWindow.h"
#include "IPropertyWidget.h"
#include "IGraphicsNodeReference.h"
#include "IElementBasicInformation.h"

#include "QWidget"
#include "QDialog"
#include "AddNewFamilyTypeDlg.h"
#include "EditComponentTypeDlg.h"
#include "FamilyTypeDialogUtils.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace Sample;

ElementId GetCurrentElementId()
{
    ElementId eleId = ElementId::InvalidID;
    if (ISelection::Get()->GetCount() == 1)
    {
        ISelection* pSelection = ISelection::Get();
        const IGraphicsNodeReference* pGraphicsNodeReference = pSelection->GetGraphicsNodeReferences().begin()->get();
        eleId = pGraphicsNodeReference->GetElementId();
        if (eleId.IsValid())
            return eleId;
    }

    // TODO 之后增加预设参数Element

    return eleId;
}

void FamilyTypeDialogUtils::ShowEditTypeDialogInternal(IUiDocument* pUIDoc, const ElementId& modelId)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pUIDoc != nullptr, L"pUIDoc指针无效",L"GDMP",L"2023-10-20");
    gcmp::IDocument* pDoc = pUIDoc->GetDbDocument();

    ElementId inspectId = modelId;

    EditComponentTypeDlg* pDlg = NEW_AS_QT_CHILD(EditComponentTypeDlg, pDoc, inspectId, dynamic_cast<QWidget*>(IMainWindow::GetMainWindow()));
    if (pDlg->exec() == QDialog::Accepted)
    {
        // 会清除选择集
        pUIDoc->UpdateView(true);
        // TODO 这里需要拿到属性面板并刷新
    }
}

// 显示类型对话框，pUIDoc为空、当前选中element为空没有类型，返回false
bool FamilyTypeDialogUtils::CmdShowTypePropertyDialog(IUiDocument* pUIDoc)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pUIDoc != nullptr, L"pUIDoc指针无效",L"GDMP",L"2023-10-20");

    ElementId selectedId = GetCurrentElementId();
    IDocument* pDoc = pUIDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc != nullptr, L"pDoc指针无效",L"GDMP",L"2023-10-20");

    IElement* pElement = pDoc->GetElement(selectedId);
    if (pElement == nullptr || pElement->GetBasicInformation()->GetType() == nullptr)
    {
        return false;
    }

    ShowEditTypeDialogInternal(pUIDoc, pElement->GetBasicInformation()->GetType()->GetElementId());
    return true;
}

void FamilyTypeDialogUtils::CmdNewFamilyTypeDialog(gcmp::IUiDocument* pUIDoc)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pUIDoc != nullptr, L"pUIDoc指针无效",L"GDMP",L"2023-10-20");

    ElementId selectedId = GetCurrentElementId();
    IDocument* pDoc = pUIDoc->GetDbDocument();

    IElement* pElement = pDoc->GetElement(selectedId);
    if (pElement == nullptr || pElement->GetBasicInformation()->GetType() == nullptr)
        return;

    IType* pInstanceType = pElement->GetBasicInformation()->GetType();

    AddNewFamilyTypeDlg dlg(pDoc, pInstanceType);
    if (dlg.exec() == QDialog::Accepted)
    {
        // TODO 这里需要拿到属性面板并刷新
    }
}

